/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared;

import avl.sv.shared.Annotations.Annotation;
import avl.sv.shared.Annotations.AnnotationSet;
import avl.sv.shared.Annotations.ROI;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Date;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableNode;

/**
 *
 * @author benbryan
 */
public class JXTreeTableROI_Model extends DefaultTreeTableModel {

    private String columnNames[] = {"Name", "visible"};
    private Class columnClass[] = {String.class, Boolean.class};
    
    private Date lastUpdate;
    public Date getLastUpdate() {
        return lastUpdate;
    }
    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }
    private transient final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
        
    public JXTreeTableROI_Model(TreeTableNode root) {
        super(root);
    }
    
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Class<?> getColumnClass(int column) {
        return columnClass[column];
    }   
    
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(Object node, int column) {
        if (node instanceof Annotation){
            Annotation annotation = (Annotation) node;
            switch (column){
                case 0:
                    return annotation.name;
                case 1:
                    return annotation.visible;
                case 2:
                    return null;
            }            
        } else if (node instanceof ROI){
            ROI roi = (ROI) node;
            switch (column){
                case 0: 
                    return roi.text;
                case 1:
                    return null;
                case 2:
                    return roi.negativeROA;
            }            
        } else if (node instanceof ClassifierClass){
            ClassifierClass classifier = (ClassifierClass) node;
            switch (column){
                case 0: 
                    return classifier.className;
                case 1:
                    return null;
                case 2:
                    return null;
            }            
        } else if (node instanceof AnnotationSet){
            AnnotationSet annoSet = (AnnotationSet) node;
            switch (column){
                case 0: 
                    return annoSet.getSlideReference().slideName;
                case 1:
                    return null;
                case 2:
                    return null;
            }            
        }
        return null;        
    }

    @Override
    public void setValueAt(Object value, Object node, int column) {
        Date currentDate = new Date();
        propertyChangeSupport.firePropertyChange("lastUpdate", lastUpdate, currentDate);
        lastUpdate = currentDate;
        if (node instanceof Annotation){
            Annotation annotation = (Annotation) node;
            AnnotationSet annoSet = (AnnotationSet) annotation.getParent();
            switch (column){
                case 0:
                    String newName = (String) value;
                    if (annotation.name.equals(newName)){
                        return;
                    }
                    if (annoSet.containsAnnotation(newName)){
                        annotation.name = newName;
                        return;
                    }
                    for (int i = 0; i < 1000; i++){
                        newName = ((String)value).concat(String.valueOf(i));
                        if (annoSet.containsAnnotation(newName)){
                            annotation.name = newName;
                            return;
                        }
                    }
                    return;
                case 1:
                    annotation.visible = (boolean) value;
                case 2:
            }            
        } else if (node instanceof ROI){
            ROI roi = (ROI) node;
            switch (column){
                case 0: 
                    roi.text = (String) value;
                    return;
                case 1:
                    return;
                case 2:
                    roi.negativeROA = (boolean) value;
            }            
        } else if (node instanceof ClassifierClass){
            ClassifierClass classifier = (ClassifierClass) node;
            switch (column){
                case 0: 
                    classifier.className = (String) value;
                case 1:
                case 2:
            }            
        }
    }

    @Override
    public boolean isCellEditable(Object node, int column) {
        if (node instanceof AnnotationSet){
            return false;
        }
        return true;
    }

    @Override
    public boolean isLeaf(Object node) {
        if (node instanceof ROI){
            return true;
        } else {
            return false;
        }
    }
    
}
